package clientcore;

import java.util.HashMap;
import clientrequest.*;

public class RequestToServerTable {
    private static HashMap<Short, Class> requestTable = null;

    public RequestToServerTable() {

    }

    public static void init(AndroidMain ac) {
        requestTable = new HashMap<Short, Class>();
        try {
            requestTable.put(Constants.CMSG_AUTH, Class.forName("clientrequest." + "RequestLogin"));
            requestTable.put(Constants.CMSG_BOARD_QUESTION, Class.forName("clientrequest." + "RequestBoardQuestion"));
        } catch (ClassNotFoundException ex) {
            ac.log("Class Not Found Exception in class RequestToServerTable in method init()");
        }
    }

    public static RequestToServer get(short request_id) {
        RequestToServer request = null;

        try {
            Class name = requestTable.get(request_id);

            if (name != null) {
                request = (RequestToServer) name.newInstance();
            } else {
                System.out.println("Invalid Request Code: " + request_id);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return request;
    }
}